﻿@page "/tag/{name}"

@if (posts == null)
{
    <Loading />
}
else
{
    <div class="post-wrap tags">
        @if (tagName != null)
        {
            <h2 class="post-title">-&nbsp;Tag&nbsp;·&nbsp;@tagName&nbsp;-</h2>
        }
    </div>
    <div class="post-wrap archive">
        @if (posts.Success && posts.Result.Any())
        {
            @foreach (var item in posts.Result)
            {
                <h3>@item.Year</h3>
                @foreach (var post in item.Posts)
                {
                    <article class="archive-item">
                        <NavLink href="@("/post"+post.Url)">@post.Title</NavLink>
                        <span class="archive-item-date">@post.CreationTime</span>
                    </article>
                }
            }
        }
        else
        {
            <ErrorTip />
        }
    </div>
}

@code {
    /// <summary>
    /// 标签名称参数
    /// </summary>
    [Parameter]
    public string name { get; set; }

    /// <summary>
    /// 标签名称
    /// </summary>
    private string tagName;

    /// <summary>
    /// 文章列表数据
    /// </summary>
    private ServiceResult<IEnumerable<QueryPostDto>> posts;

    /// <summary>
    /// 初始化
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        // TODO:获取数据,可以在API中合并这两个请求。
        var tag = await Http.GetFromJsonAsync<ServiceResult<string>>($"/blog/tag?name={name}");
        posts = await Http.GetFromJsonAsync<ServiceResult<IEnumerable<QueryPostDto>>>($"/blog/posts/tag?name={name}");

        if (tag.Success)
        {
            tagName = tag.Result;

            await Common.SetTitleAsync(tagName);
        }
    }
}